package com.zzrc.grape.service.impl;

import com.google.gson.Gson;
import com.zzrc.grape.dao.mapper.UserDao;
import com.zzrc.grape.model.Key;
import com.zzrc.grape.model.Log;
import com.zzrc.grape.model.User;
import com.zzrc.grape.service.UserService;
import com.zzrc.grape.util.*;
import com.zzrc.lotus.dao.mapper.BankCardMapper;
import com.zzrc.lotus.dao.mapper.UserBaseMapper;
import com.zzrc.lotus.dao.mapper.UserIdentityMapper;
import com.zzrc.lotus.model.db.*;
import com.zzrc.peony.dao.mapper.LogUserStatusMapper;
import com.zzrc.peony.dao.mapper.RecordLoanMapper;
import com.zzrc.peony.dao.mapper.UserAccountMapper;
import com.zzrc.peony.model.db.*;
import org.hibernate.validator.constraints.pl.REGON;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;

/**
 * Service实现类
 * @author Maochao-zhu
 *
 */
@Service
public class UserServiceImpl implements UserService {
	@Resource
	UserBaseMapper userBaseMapper;

	@Resource
	UserAccountMapper userAccountMapper;

	@Resource
	UserIdentityMapper userIdentityMapper;

	@Resource
	BankCardMapper bankCardMapper;

	@Resource
	LogUserStatusMapper logUserStatusMapper;

	@Resource
	RecordLoanMapper recordLoanMapper;


	@Override
	public BasePageData deleteUser(String mobile) {
		BasePageData basePageData = new BasePageData();
		basePageData.setData(new Date());
		Connection conn;
		try {
			conn = JDBCUtil.getConnection();
			String userGid = selectUserGidByMobile(mobile);
			if (userGid == null) {
				basePageData.setMsg("数据库中没有此用户信息");
			} else {
				deleteUserInfoByUserGid(conn, userGid);
				basePageData.setMsg(WebResponseCode.SUCCESSINFO);
			}
			basePageData.setCode(WebResponseCode.SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			basePageData.setCode(WebResponseCode.FAIL);
			basePageData.setMsg("执行sql失败：" + e.toString());
			return basePageData;
		}

		return basePageData;
	}

	@Override
	public BasePageData resetUserAccount(String mobile) {
		BasePageData basePageData = new BasePageData();
		basePageData.setData(new Date());
		UserBaseExample example = new UserBaseExample();
		UserBaseExample.Criteria criteria = example.createCriteria();
		criteria.andMobileEqualTo(mobile);
		List<UserBase> userBases = userBaseMapper.selectByExample(example);
		if (userBases.size() > 0) {
			UserBase userBase = userBases.get(0);
			String userGid = userBase.getGid();
			UserAccountExample userAccountExample = new UserAccountExample();
			UserAccountExample.Criteria  criteriaUserAccount = userAccountExample.createCriteria();
			criteriaUserAccount.andUserGidEqualTo(userGid);
			List<UserAccount> userAccounts = userAccountMapper.selectByExample(userAccountExample);
			if (userAccounts.size() > 0) {
				int creditAmount = userAccounts.get(0).getCreditAmount();
				UserAccount userAccount = new UserAccount();
				userAccount.setCurrentBorrowedAmount(0);
				userAccount.setCanBorrowAmount(creditAmount);

				userAccountMapper.updateByExampleSelective(userAccount, userAccountExample);
				basePageData.setCode(WebResponseCode.SUCCESS);
				basePageData.setMsg(WebResponseCode.SUCCESSINFO);
			} else {
				basePageData.setCode(WebResponseCode.FAIL);
				basePageData.setMsg("user_account表没有此用户信息");
			}
		} else {
			basePageData.setCode(WebResponseCode.FAIL);
			basePageData.setMsg("user_base表没有此用户信息");
		}

		return basePageData;
	}

	/**
	 * 查询用户基础信息
	 * @param mobile 手机号
	 * @return BasePageData
	 */
	@Override
	public BasePageData getUserBaseList(String mobile) {
		BasePageData basePageData = new BasePageData();
		UserBaseExample example = new UserBaseExample();
		UserBaseExample.Criteria criteria = example.createCriteria();
		criteria.andMobileEqualTo(mobile);
		List<UserBase> userBases = userBaseMapper.selectByExample(example);
		basePageData.setCode(WebResponseCode.SUCCESS);
		basePageData.setMsg(WebResponseCode.SUCCESSINFO);
		basePageData.setData(userBases);
		return basePageData;
	}

	/**
	 * 查询用户实名信息
	 * @param mobile 手机号
	 * @return BasePageData
	 */
	@Override
	public BasePageData getUserIdentityList(String mobile) {
		BasePageData basePageData = new BasePageData();
		String userGid = selectUserGidByMobile(mobile);
		UserIdentityExample example = new UserIdentityExample();
		UserIdentityExample.Criteria criteria = example.createCriteria();
		criteria.andUserGidEqualTo(userGid);
		List<UserIdentity> userIdentities = userIdentityMapper.selectByExample(example);
		basePageData.setCode(WebResponseCode.SUCCESS);
		basePageData.setMsg(WebResponseCode.SUCCESSINFO);
		basePageData.setData(userIdentities);
		return basePageData;
	}

	/**
	 * 查询用户银行卡信息
	 * @param mobile 手机号
	 * @return BasePageData
	 */
	@Override
	public BasePageData getBankCardList(String mobile) {
		BasePageData basePageData = new BasePageData();
		String userGid = selectUserGidByMobile(mobile);
		BankCardExample example = new BankCardExample();
		BankCardExample.Criteria criteria = example.createCriteria();
		criteria.andUserGidEqualTo(userGid);
		List<BankCard> bankCards = bankCardMapper.selectByExample(example);
		basePageData.setCode(WebResponseCode.SUCCESS);
		basePageData.setMsg(WebResponseCode.SUCCESSINFO);
		basePageData.setData(bankCards);
		return basePageData;
	}

	/**
	 * 查询用户授信信息
	 * @param mobile 手机号
	 * @return BasePageData
	 */
	@Override
	public BasePageData getLogUserStatusList(String mobile) {
		BasePageData basePageData = new BasePageData();
		String userGid = selectUserGidByMobile(mobile);
		LogUserStatusExample example = new LogUserStatusExample();
		LogUserStatusExample.Criteria criteria = example.createCriteria();
		criteria.andUserGidEqualTo(userGid);
		List<LogUserStatus> logUserStatuses = logUserStatusMapper.selectByExample(example);
		basePageData.setCode(WebResponseCode.SUCCESS);
		basePageData.setMsg(WebResponseCode.SUCCESSINFO);
		basePageData.setData(logUserStatuses);
		return basePageData;
	}

	/**
	 * 查询用户额度信息
	 * @param mobile 手机号
	 * @return BasePageData
	 */
	@Override
	public BasePageData getUserAccountList(String mobile) {
		BasePageData basePageData = new BasePageData();
		String userGid = selectUserGidByMobile(mobile);
		UserAccountExample example = new UserAccountExample();
		UserAccountExample.Criteria criteria = example.createCriteria();
		criteria.andUserGidEqualTo(userGid);
		List<UserAccount> userAccounts = userAccountMapper.selectByExample(example);
		basePageData.setCode(WebResponseCode.SUCCESS);
		basePageData.setMsg(WebResponseCode.SUCCESSINFO);
		basePageData.setData(userAccounts);
		return basePageData;
	}

	/**
	 * 查询用户借款信息
	 * @param mobile 手机号
	 * @return BasePageData
	 */
	@Override
	public BasePageData getRecordLoanList(String mobile) {
		BasePageData basePageData = new BasePageData();
		String userGid = selectUserGidByMobile(mobile);
		RecordLoanExample example = new RecordLoanExample();
		RecordLoanExample.Criteria criteria = example.createCriteria();
		criteria.andUserGidEqualTo(userGid);
		List<RecordLoan> recordLoans = recordLoanMapper.selectByExample(example);
		basePageData.setCode(WebResponseCode.SUCCESS);
		basePageData.setMsg(WebResponseCode.SUCCESSINFO);
		basePageData.setData(recordLoans);
		return basePageData;
	}

	public String selectUserGidByMobile(String mobile) {
		UserBaseExample example = new UserBaseExample();
		UserBaseExample.Criteria criteria = example.createCriteria();
		criteria.andMobileEqualTo(mobile);
		List<UserBase> userBases = userBaseMapper.selectByExample(example);
		if (userBases.size()>0){
			return userBases.get(0).getGid();
		}
		return null;

	}

	public void deleteUserInfoByUserGid(Connection conn, String userGid) throws SQLException {

		// 用户相关
		JDBCUtil.deleteSQL("delete from lotus.user_base where gid =?", conn, userGid);
		JDBCUtil.deleteSQL("delete from lotus.bank_account where user_gid =?", conn, userGid);
		JDBCUtil.deleteSQL("delete from lotus.bank_card where user_gid =?", conn, userGid);
		JDBCUtil.deleteSQL("delete from lotus.log_user_change where user_gid =?", conn, userGid);
		JDBCUtil.deleteSQL("delete from lotus.operate_bank_card where user_gid =?", conn, userGid);
		JDBCUtil.deleteSQL("delete from lotus.user_face_verify where user_gid =?", conn, userGid);
		JDBCUtil.deleteSQL("delete from lotus.user_identity where user_gid =?", conn, userGid);
		JDBCUtil.deleteSQL("delete from peony.log_user_account where user_gid =?", conn, userGid);
		JDBCUtil.deleteSQL("delete from peony.log_user_status where user_gid =?", conn, userGid);
		JDBCUtil.deleteSQL("delete from peony.user_account where user_gid =?", conn, userGid);
		JDBCUtil.updateSQL("update  peony.config_user_whitelist set user_gid = null where user_gid =?", conn, userGid);
		JDBCUtil.deleteSQL("delete from peony.user_bestsign_account where user_gid =?", conn, userGid);
		JDBCUtil.deleteSQL("delete from peony.record_credit_status where user_gid =?", conn, userGid);
		JDBCUtil.deleteSQL("delete from peony.record_pact_user where user_gid =?", conn, userGid);

		// 订单相关
		JDBCUtil.deleteSQL("delete from peony.record_contact_info where user_gid =?", conn, userGid);
		JDBCUtil.deleteSQL("delete from peony.record_family_info where user_gid =?", conn, userGid);
		JDBCUtil.deleteSQL("delete from peony.record_loan where user_gid =?", conn, userGid);
		JDBCUtil.deleteSQL("delete from peony.record_pact_loan where user_gid =?", conn, userGid);
		JDBCUtil.deleteSQL("delete from peony.record_pre_loan where user_gid =?", conn, userGid);
		JDBCUtil.deleteSQL("delete from peony.record_repayment where user_gid =?", conn, userGid);
		JDBCUtil.deleteSQL("delete from peony.record_repayment_plan where user_gid =?", conn, userGid);
		JDBCUtil.deleteSQL("delete from peony.trans_deduction where user_gid =?", conn, userGid);
		JDBCUtil.deleteSQL("delete from peony.trans_withdraw where user_gid =?", conn, userGid);

		// 信审相关
		JDBCUtil.deleteSQL("delete from plum.log_audit_opt where loan_gid in (select gid from peony.record_loan where user_gid=?)", conn, userGid);
		JDBCUtil.deleteSQL("delete from plum.record_audit_opt where loan_gid in (select gid from peony.record_loan where user_gid=?)", conn, userGid);
	}

	@Override
	public BasePageData addWhiteList(String idCard) throws Exception {
		BasePageData data = new BasePageData();
		// 构造请求参数
		Map<String, Object> params = new HashMap<>();
		params.put("operatorId", "1111323243");
		List<Map<String, Object>> whitelist = new ArrayList<>();
		Map<String, Object> param = new HashMap<>();
		param.put("idcardNo", idCard);
		param.put("recommendAmount", 0);
		param.put("salesmanGid", "202004281234");
		param.put("salesmanName", "王2狗");
		whitelist.add(param);
		params.put("whitelist", whitelist);
		params.put("operatorId", "1111323243");
		String result = HttpClientUtils.doPostJson("http://ebjqa.zzrcer.com/peony/aksk/salesman/saveWhitelist", new Gson().toJson(params));

		if (result == null) {
			data.setCode(WebResponseCode.ERROR);
			data.setMsg("响应为空，请检查添加白名单接口是否有变动。");
		} else {
			// 处理请求结果
			Map map = new Gson().fromJson(result, Map.class);
			System.out.println(map.toString());
			Double code = (Double) map.get("code");
			data.setCode(code.intValue());
			data.setMsg((String) map.get("message"));
			data.setData(map.get("body"));
		}


		return data;
	}
}
